﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
using CUWo.App.Resources;
using CUWo.Common;
using CUWo.Common.Entities;
using CUWo.Common.StorageHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;

namespace CUWo.App.CustomControl.UserCenter
{
    public partial class UCUserCenterSetting : UserControl
    {
        public UCUserCenterSetting()
        {
            InitializeComponent();
        }

        private void gridMarketplaceReview_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                //创建一个查看本应用程序简介和评论的任务
                MarketplaceReviewTask mdr = new MarketplaceReviewTask();
                mdr.Show();
            }
            catch
            {
                //MessageToastManager.Show(AppResources.MainNotFoundApplication);
            }
        }

        private void gridQA_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Visible;
        }

        private void gridSuggest_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Visible;
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void gridAbout_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.gridSettingMain.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingSuggest.Visibility = System.Windows.Visibility.Collapsed;
            this.gridSettingAbout.Visibility = System.Windows.Visibility.Visible;
            this.gridSettingQA.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void tgsPush_Checked(object sender, RoutedEventArgs e)
        {
            //TextBlock content = FindVisualChild(this.tgsPush, "txtToggleSwitchStatus");
            //if (content != null)
            //    content.Text = "开启";

        }

        private void tgsPush_Unchecked(object sender, RoutedEventArgs e)
        {
            //TextBlock content = FindVisualChild(this.tgsPush, "txtToggleSwitchStatus");
            //if (content != null)
            //    content.Text = "关闭";
        }

        public TextBlock FindVisualChild(DependencyObject obj, string childItemName)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is TextBlock)
                {
                    LogHelper.LogInfo(":::" + child.GetType().FullName + "\t" + ((TextBlock)child).Text);
                    if (childItemName.Equals((child as TextBlock).Name))
                    {
                        return child as TextBlock;
                    }
                }
                else
                {
                    TextBlock childOfChild = FindVisualChild(child, childItemName);
                    if (childOfChild != null)
                    {
                        LogHelper.LogInfo(">>>" + childOfChild.GetType().FullName);
                        if (childItemName.Equals((childOfChild as TextBlock).Name))
                        {
                            return childOfChild;
                        }
                    }
                }
            }
            return null;
        }

        private void gridClearCache_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            RemoveIsolateStorageByAll();
        }

        /// <summary>
        /// 移除所有缓存信息
        /// </summary>
        public static void RemoveIsolateStorageByAll()
        {
            try
            {
                if (!IsolatedStorageHelper.Instance.DirectoryExists(Keys.ChinaUnicomeCacheDirectory))
                    return;

                bool result = IsolatedStorageHelper.Instance.DeleteDirectory(Keys.ChinaUnicomeCacheDirectory, true);
                if (result)
                {
                    MessageToastManager.Show("清理缓存成功！", MessageToastLevel.Info);
                }
                else
                {

                }
                //
                //IsolatedStorageSettings.ApplicationSettings.Clear();
            }
            catch (Exception err)
            {
                LogHelper.LogError("清理缓存异常" + Environment.NewLine + err.Message, typeof(UCUserCenterSetting), MethodBase.GetCurrentMethod());
                MessageToastManager.Show("清理缓存异常！", MessageToastLevel.Error);
            }
        }

        #region 
        private void imgUserExit_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if(!IsolatedStorageHelper.Instance.FileExists(Keys.ROM_FILENAME))
            {
                MessageToastManager.Show("您还未登录！");
                return;
            }
            if (MessageBoxResult.OK != MessageBox.Show("退出后部分功能将无法使用，确认退出？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel))
            {
                return;
            }
            var romInfoString = IsolatedStorageHelper.Instance.ReadText(Keys.ROM_FILENAME);
            if (!string.IsNullOrWhiteSpace(romInfoString))
            {
                RomInfo romInfo = JSONHelper.Deserialize<RomInfo>(romInfoString);
                if (romInfo != null)
                {
                    romInfo.Mobile = string.Empty;
                    romInfo.Password = string.Empty;
                }
                IsolatedStorageHelper.Instance.WriteText(JSONHelper.Serialize<RomInfo>(romInfo), Keys.ROM_FILENAME, true);
            }
            MessageToastManager.Show("操作成功，部分功能将无法使用！",MessageToastLevel.Warn);
        }
        #endregion
    }
}
